package com.wr.recruit.service.apis;

import com.google.common.collect.Lists;
import com.wr.recruit.service.apis.dto.DepartmentDto;
import com.wr.recruit.service.dao.dbo.DepartmentDbo;
import com.wr.recruit.service.dao.dbo.UserDbo;
import com.wr.recruit.service.service.DepartmentService;
import com.wr.recruit.service.service.UserService;
import com.wr.recruit.service.utils.APIResponse;
import com.wr.recruit.service.utils.APIResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;


@Controller
public class DepartmentApis {

    private DepartmentService departmentService;
    private UserService userService;

    @Autowired
    public DepartmentApis(DepartmentService departmentService,
                          UserService userService) {
        this.departmentService = departmentService;
        this.userService = userService;
    }

    @PostMapping(value = "/department/create", produces = "application/json")
    @ResponseBody
    public APIResponse rsDepartmentCreate(@RequestBody DepartmentDto departmentDto){
        if (Objects.isNull(departmentDto.getUserName())
                || "".equals(departmentDto.getUserName()))
            return new APIResponseSet(false,"创建部门失败,请填写创建用户");
        UserDbo userInfo = userService.getUserInfo(departmentDto.getUserName());
        if (Objects.isNull(userInfo))
            return new APIResponseSet(false,"创建部门失败,无此用户");
        DepartmentDbo departmentDbo = new DepartmentDbo();
        departmentDbo.setRevoked(false);
        departmentDbo.setCreatedAt(new Timestamp(new Date().getTime()));
        departmentDbo.setDepartmentCode(UUID.randomUUID().toString());
        departmentDbo.setDepartmentName(departmentDto.getDepartmentName());
        departmentDbo.setDescription(departmentDto.getDescription());
        departmentDbo.setCompanyCode(userInfo.getCompanyCode());
        DepartmentDbo department = departmentService.createDepartment(departmentDbo);
        if (Objects.isNull(department))
            return new APIResponseSet(false,"创建部门失败,存在相同部门");
        return new APIResponseSet(true,"创建部门成功", Lists.newArrayList(department));
    }

    @PostMapping(value = "/department/update", produces = "application/json")
    @ResponseBody
    public APIResponse rsDepartmentUpdate(@RequestBody DepartmentDto departmentDto){
        if (Objects.isNull(departmentDto.getUserName())
                || "".equals(departmentDto.getUserName()))
            return new APIResponseSet(false,"创建部门失败,请填写创建用户");
        UserDbo userInfo = userService.getUserInfo(departmentDto.getUserName());
        if (Objects.isNull(userInfo))
            return new APIResponseSet(false,"创建部门失败,无此用户");
        if (Objects.isNull(departmentDto.getDepartmentCode())
                || "".equals(departmentDto.getDepartmentCode()))
            return new APIResponseSet(false,"创建部门失败,请填写选择部门");
        DepartmentDbo departmentDbo =
                departmentService.getDepartment(departmentDto.getDepartmentCode(),userInfo.getCompanyCode());
        departmentDbo.setDepartmentName(departmentDto.getDepartmentName());
        departmentDbo.setDescription(departmentDto.getDescription());
        DepartmentDbo departmentDboUpdate = departmentService.updateDepartment(departmentDbo);
        if (Objects.isNull(departmentDboUpdate))
            return new APIResponseSet(false,"创建部门失败,存在相同部门");
        return new APIResponseSet(true,"创建部门成功", Lists.newArrayList(departmentDboUpdate));
    }

    @PostMapping(value = "/department/delete", produces = "application/json")
    @ResponseBody
    public APIResponse rsDepartmentDelete(@RequestBody DepartmentDto departmentDto){
        departmentService.deleteDepartment(departmentDto.getDepartmentCode());
        return new APIResponseSet(true,"删除部门成功");
    }

    @GetMapping(value = "/department/create", produces = "application/json")
    @ResponseBody
    public APIResponse rsDepartmentListGet(@RequestParam(value = "companyCode") String companyCode,
                                           @RequestParam(value = "departmentName", required = false) String departmentName,
                                           @RequestParam(value = "pageSize") Integer pageSize,
                                           @RequestParam(value = "pageNum") Integer pageNum){
        Page<DepartmentDbo> departmentList = departmentService.getDepartmentList(companyCode, departmentName, pageSize, pageNum);
        return new APIResponseSet(true,"查询部门成功", Lists.newArrayList(departmentList));
    }

    @PostMapping(value = "/department/user/create", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserDepartmentCreate(@RequestBody DepartmentDto departmentDto){
        Boolean result = departmentService.updateUserDepartment(departmentDto.getDepartmentCode(), departmentDto.getUserName());
        if (result)
            return new APIResponseSet(true,"成功进入部门");
        return new APIResponseSet(false,"进入部门失败");
    }

    @GetMapping(value = "/department/user/list", produces = "application/json")
    @ResponseBody
    public APIResponse rsUserDepartmentList(@RequestParam(value = "companyCode") String companyCode,
                                            @RequestParam(value = "departmentCode") String departmentCode){
        List<UserDbo> userDepartment = departmentService.getUserDepartment(companyCode, departmentCode);
        if (Objects.isNull(userDepartment))
            return new APIResponseSet(false,"查询部门人员失败");
        return new APIResponseSet(true,"查询部门人员成功", userDepartment);
    }
}
